package controller

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"myproject/model"
	"myproject/schemas"
)

// 总路由分发
func NewsRegister(newsGrp *gin.RouterGroup) {
	newsGrp.Use().POST("/addnews", addNews)
	newsGrp.Use().GET("/getnews", getNews)
	newsGrp.Use().POST("/adduser", addUsers)
	newsGrp.Use().GET("/reviewnews", reviewNews)

}

// 添加用户
func addUsers(c *gin.Context) {
	users := &schemas.Users{}
	_ = c.Bind(users)

	if users.Name == "" {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "数据不全",
		})
	} else if users.UType == 1 {
		sql := "INSERT INTO users(name,Type) values (?,?)"
		model.DB.Exec(sql, users.Name, users.UType)
		c.JSON(200, gin.H{"msg": "管理员设置成功"})
	} else {
		sql := "INSERT INTO users(name) values (?)"
		model.DB.Exec(sql, users.Name)
		c.JSON(200, gin.H{"msg": "添加成功"})
	}

}

// 添加新闻
func addNews(c *gin.Context) {
	fmt.Println("添加新闻")
	news := &schemas.News{}
	_ = c.Bind(news)
	// 查询管理员人数并且上班
	var caudit []schemas.GetUsers
	sql := "select * from users where type=1 and status=1"
	model.DB.Raw(sql).Scan(&caudit)

	//================================================================
	// 获取新闻最大的id
	//var maxuid schemas.GetNews
	//model.DB.Raw("select max(id) from news").Scan(&maxuid)
	//fmt.Println("maxuid", maxuid)
	//
	//var audit_id = 0
	//
	//_ = audit_id
	//
	//if len(caudit) == 0 {
	//	audit_id = 1
	//} else {
	//	audit_id = maxuid.Id
	//}
	//fmt.Println("audit_id", audit_id)
	//================================================================

	//添加新闻
	res := model.DB.Exec("INSERT INTO news(Title,Content,Userid ) values(?,?,?)", news.Title, news.Content, news.Userid)
	if res.Error != nil {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		fmt.Println("err", res.Error)
		return
	}
	// 获取新闻最大的id
	var newslist []schemas.GetNews
	sql1 := "select id from news"
	model.DB.Raw(sql1).Scan(&newslist)
	fmt.Println(sql1)
	maxnid := newslist[len(newslist)-1].Id
	fmt.Println("maxnid", maxnid)

	//新闻最大id%管理员上班总人数-》取模
	auditId := maxnid % len(caudit)
	fmt.Println("audit_id", auditId, "len(caudit)", len(caudit))

	//	自动修改新闻表审核员字段
	sql2 := "update news set Audit_id=? where id=?"
	model.DB.Exec(sql2, caudit[auditId].Id, maxnid)
	fmt.Println(sql2)
}

// 获取新闻
func getNews(c *gin.Context) {
	Id := c.Query("uid")
	//fmt.Println("Id", Id)
	news := &schemas.GetNews{}
	_ = c.Bind(news)
	fmt.Println("获取新闻")
	if Id == "" {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "先登录",
		})
		return
	}
	// 获取用户身份
	var place schemas.GetUserPlace
	sql := "select * from users where id=?"
	model.DB.Raw(sql, Id).Scan(&place)
	if place.Id == 0 {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "没有找到用户",
		})
		return
	}
	fmt.Println(place)
	// 判断user身份
	var newslist []schemas.GetNews
	if place.Type == false {
		// 查询所有审核的数据
		sql3 := "select * from news where Type=?"
		model.DB.Raw(sql3, 1).Scan(&newslist)
		fmt.Println("sql>", sql3, "newslist", newslist)
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "ok",
			"data": newslist,
		})
	} else {
		// 查询管理员对应的并且没审核的数据
		sql4 := "select * from news where Type=? and Audit_id=?"
		model.DB.Raw(sql4, 0, place.Id).Scan(&newslist)
		fmt.Println("sql4>", sql4, "newslist", newslist)
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "ok",
			"data": newslist,
		})
	}

}

// 审核新闻
func reviewNews(c *gin.Context) {
	nid := c.Query("nid")
	nstatus := c.Query("status")
	if nid == "" || nstatus == "" {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "参数错误",
		})
		fmt.Println(nid, nstatus)
		return
	}
	sql := "update news set type = ? where id = ?"
	err := model.DB.Exec(sql, nstatus, nid)
	if err.Error != nil {
		c.JSON(400, gin.H{
			"code": 400,
		})
		fmt.Println(err.Error)
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
	})

}
